package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <pre>
 * 考生技能考试分组信息表
 * </pre>
 * <small> 2020-06-05 09:41:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examineeGroup")
public class ExamineeGroupController extends BaseController {
    @Autowired
    private ExamineeGroupService examineeGroupService;
    @Autowired
    private StuSignUpService stuSignUpService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private ExaminationBatchService examinationBatchService;
    @Autowired
    private ExaminationPaperService examinationPaperService;

    @GetMapping()
    @RequiresPermissions("jzweb:examineeGroup:examineeGroup")
    String ExamineeGroup() {
        return "jzweb/examineeGroup/examineeGroup";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:examineeGroup:examineeGroup")
    public Result<Page<ExamineeGroupDO>> list(ExamineeGroupDO examineeGroupDTO) {
        Wrapper<ExamineeGroupDO> wrapper = new EntityWrapper<ExamineeGroupDO>().orderBy("id", false);
        if (StringUtils.isNotBlank(examineeGroupDTO.getExaminRoomNum())) {
            wrapper.eq("examin_room_num", examineeGroupDTO.getExaminRoomNum());
        }
        if (StringUtils.isNotBlank(examineeGroupDTO.getGroupNum())) {
            wrapper.eq("group_num", examineeGroupDTO.getGroupNum());
        }
        if (StringUtils.isNotBlank(examineeGroupDTO.getStationNum())) {
            wrapper.eq("station_num", examineeGroupDTO.getStationNum());
        }
        if (StringUtils.isNotBlank(examineeGroupDTO.getStuSignName())) {
            Wrapper<StuSignUpDO> stuWrapper = new EntityWrapper<StuSignUpDO>().like("name", examineeGroupDTO.getStuSignName());
            List<StuSignUpDO> stuSignUpDOS = stuSignUpService.selectList(stuWrapper);
            String str = "";
            for (StuSignUpDO stuSignUpDO : stuSignUpDOS) {
                str = str + stuSignUpDO.getId() + ",";
            }
            wrapper.and();
            if (StringUtils.isNotBlank(str)) {
                wrapper.in("stu_sign_id", str.substring(0, str.length() - 1));
            } else {
                wrapper.eq("stu_sign_id", null);
            }
        }
        if (StringUtils.isNotBlank(examineeGroupDTO.getOrgName())) {
            Wrapper<OrganizationNewsDO> orgWrapper = new EntityWrapper<OrganizationNewsDO>().like("name", examineeGroupDTO.getOrgName());
            List<OrganizationNewsDO> organizationNewsDOS = organizationNewsService.selectList(orgWrapper);
            String str = "";
            for (OrganizationNewsDO organizationNewsDO : organizationNewsDOS) {
                str = str + organizationNewsDO.getId() + ",";
            }
            wrapper.and();
            if (StringUtils.isNotBlank(str)) {
                wrapper.in("org_id", str.substring(0, str.length() - 1));
            } else {
                wrapper.eq("org_id", null);
            }
        }
        if (StringUtils.isNotBlank(examineeGroupDTO.getEbatchName())) {
            Wrapper<ExaminationBatchDO> exaWrapper = new EntityWrapper<ExaminationBatchDO>().like("name", examineeGroupDTO.getEbatchName());
            List<ExaminationBatchDO> examinationBatchDOS = examinationBatchService.selectList(exaWrapper);
            String str = "";
            for (ExaminationBatchDO examinationBatchDO : examinationBatchDOS) {
                str = str + examinationBatchDO.getId() + ",";
            }
            wrapper.and();
            if (StringUtils.isNotBlank(str)) {
                wrapper.in("ebatch_id", str.substring(0, str.length() - 1));
            } else {
                wrapper.eq("ebatch_id", null);
            }
        }

        Page<ExamineeGroupDO> page = examineeGroupService.selectPage(getPage(ExamineeGroupDO.class), wrapper);
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            ExamineeGroupDO obj = (ExamineeGroupDO) page.getRecords().get(i);
            if (obj.getStuSignId() != null) {
                StuSignUpDO stuSignUpDO = stuSignUpService.selectById(obj.getStuSignId());
                if (stuSignUpDO != null) {
                    obj.setStuSignName(stuSignUpDO.getName());
                }
            }
            if (obj.getOrgId() != null) {
                OrganizationNewsDO organizationNewsDO = organizationNewsService.selectById(obj.getOrgId());
                if (organizationNewsDO != null) {
                    obj.setOrgName(organizationNewsDO.getName());
                }
            }
            if (obj.getEbatchId() != null) {
                ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(obj.getEbatchId());
                if (examinationBatchDO != null) {
                    obj.setEbatchName(examinationBatchDO.getName());
                }
            }
            if (obj.getEpaperId() != null) {
                ExaminationPaperDO examinationPaperDO = examinationPaperService.selectById(obj.getEpaperId());
                if (examinationPaperDO != null) {
                    obj.setEpaperName(examinationPaperDO.getName()+"("+examinationPaperDO.getNumbers()+")");
                }
            }
            if (obj.getCreaterId() != null) {
                UserDO userDO = userService.selectById(obj.getCreaterId());
                if (userDO != null) {
                    obj.setCreaterName(userDO.getName());
                }
            }
            if (obj.getUpdaterId() != null) {
                UserDO userDO = userService.selectById(obj.getUpdaterId());
                if (userDO != null) {
                    obj.setUpdaterName(userDO.getName());
                }
            }
            list.add(obj);
        }
        page.setRecords(list);
        return Result.ok(page);
    }

    @GetMapping("/add")
    @RequiresPermissions("jzweb:examineeGroup:add")
    String add() {
        return "jzweb/examineeGroup/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("jzweb:examineeGroup:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        ExamineeGroupDO examineeGroup = examineeGroupService.selectById(id);
        model.addAttribute("examineeGroup", examineeGroup);
        return "jzweb/examineeGroup/edit";
    }

    @Log("添加考生技能考试分组信息表")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("jzweb:examineeGroup:add")
    public Result<String> save(ExamineeGroupDO examineeGroup) {
        examineeGroupService.insert(examineeGroup);
        return Result.ok();
    }

    @Log("修改考生技能考试分组信息表")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("jzweb:examineeGroup:edit")
    public Result<String> update(ExamineeGroupDO examineeGroup) {
        boolean update = examineeGroupService.updateById(examineeGroup);
        return update ? Result.ok() : Result.fail();
    }

    @Log("删除考生技能考试分组信息表")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("jzweb:examineeGroup:remove")
    public Result<String> remove(Long id) {
        examineeGroupService.deleteById(id);
        return Result.ok();
    }

    @Log("批量删除考生技能考试分组信息表")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("jzweb:examineeGroup:batchRemove")
    public Result<String> remove(@RequestParam("ids[]") Long[] ids) {
        examineeGroupService.deleteBatchIds(Arrays.asList(ids));
        return Result.ok();
    }

}
